package me.zhengjie.modules.system.service.apply;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.exception.CommonUtilException;
import me.zhengjie.modules.system.domain.DisUserRight;
import me.zhengjie.modules.system.domain.IdentityCourse;
import me.zhengjie.modules.system.domain.IdentityItem;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.enums.ApplyWorkOrderTypeEnum;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.DisChangeApplyDto;
import me.zhengjie.modules.system.service.dto.WxUserDto;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.service.mybatisplus.IDisUserRightPlusService;
import me.zhengjie.modules.web.service.mybatisplus.IdentityCoursePlusService;
import me.zhengjie.modules.web.service.mybatisplus.IdentityItemPlusService;
import me.zhengjie.modules.web.service.pay.OrderSubmitFactory;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class IdentityChangeServiceImpl implements IApplyCallbackService {

    @Autowired
    WxUserService wxUserService;

    @Autowired
    IDisUserRightPlusService disUserRightPlusService;

    @Autowired
    IdentityItemPlusService identityItemPlusService;

    @Autowired
    IdentityCoursePlusService identityCoursePlusService;

    @Override
    public Integer getApplyType() {
        return ApplyWorkOrderTypeEnum.IdentityChange.getType();
    }

    @Override
    public boolean execute(DisChangeApplyDto applyDto) {
        String json = applyDto.getApplyContent();
        if (StringUtils.isEmpty(json)) {
            throw new CommonUtilException("申请内容不能为空");
        }
        WxUser wxUser = JSONUtil.toBean(json, WxUser.class);
        if (StringUtils.isBlank(wxUser.getIdentityNo()) || Objects.isNull(wxUser.getId())) {
            throw new CommonUtilException("申请内容字段缺少");
        }
        LambdaQueryWrapper<IdentityItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdentityItem::getItemNo, wxUser.getIdentityNo());
        wrapper.eq(IdentityItem::getStatus, ConstantUtils.Valid);
        IdentityItem identityItem = identityItemPlusService.getOne(wrapper);
        if (Objects.isNull(identityItem)) {
            throw new CommonUtilException("身份产品不存在");
        }
        try {
            wxUser.setTicketNumber(identityItem.getTicketNum());
            wxUserService.update(wxUser);
            //增加身份权益
            WxUserDto wxUserDTO = wxUserService.findById(wxUser.getId());
            LambdaQueryWrapper<IdentityCourse> courseWrapper = new LambdaQueryWrapper<>();
            courseWrapper.eq(IdentityCourse::getIdentityNo, wxUser.getIdentityNo());
            List<IdentityCourse> list = identityCoursePlusService.list(courseWrapper);
            if (CollectionUtils.isNotEmpty(list)){
                LambdaQueryWrapper<DisUserRight> rightWrapper = new LambdaQueryWrapper<>();
                rightWrapper.eq(DisUserRight::getRightType,2);
                rightWrapper.eq(DisUserRight::getOpenId,wxUserDTO.getOpenId());
                rightWrapper.eq(DisUserRight::getRightCategory,0);
                rightWrapper.eq(DisUserRight::getRightStatus,ConstantUtils.Valid);
                disUserRightPlusService.remove(rightWrapper);
                //添加
                list.forEach(s -> {
                    DisUserRight userRight = new DisUserRight();
                    userRight.setOpenId(wxUserDTO.getOpenId());
                    userRight.setRightCategory(0);
                    userRight.setRightType(2);
                    userRight.setRightContent(s.getCourseNo());
                    userRight.setRightStatus(ConstantUtils.Valid);
                    disUserRightPlusService.save(userRight);
                });

                //增加个人中心课程
                list.forEach(s -> {
                    OrderSubmitRequest requestEntity = new OrderSubmitRequest();
                    requestEntity.setCourseNo(s.getCourseNo());
                    requestEntity.setOrderType(OrderTypeEnum.Course.getOrderType());
                    requestEntity.setBackendOpenId(wxUserDTO.getOpenId());
                    requestEntity.setNotNeedPay(true);
                    List<OrderSubmitRequest.CourseApplicantsRequest> courseApplicantsRequests = new ArrayList<>();
                    OrderSubmitRequest.CourseApplicantsRequest applicantsRequest = new OrderSubmitRequest.CourseApplicantsRequest();
                    applicantsRequest.setMobile(wxUserDTO.getMobile());
                    applicantsRequest.setRealName(wxUserDTO.getRealName());
                    courseApplicantsRequests.add(applicantsRequest);
                    requestEntity.setApplicantsList(courseApplicantsRequests);
                    requestEntity.setGrantFlag(ApplyWorkOrderTypeEnum.IdentityChange.name());
                    OrderSubmitResponse execute = OrderSubmitFactory.orderType(requestEntity.getOrderType()).execute(requestEntity);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
